package core.devin.com.dmapplication;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.sdk.android.push.AndroidPopupActivity;
import com.alibaba.sdk.android.push.common.util.JSONUtils;

import org.apache.cordova.PluginResult;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class PopupPushActivity extends AndroidPopupActivity {
    static final String TAG = "PopupPushActivity";

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
    }

    public void onMessage(Intent intent) {
        String body = intent.getStringExtra("body");
        Bundle bundle = intent.getExtras();
        Log.i(TAG,"[AMS]Receive notification, body: " + bundle.toString());

        try {
            JSONObject e = new JSONObject(body);
            Map noticeMap = JSONUtils.toMap(e);

            String title = (String)noticeMap.get("title");
            String content = (String)noticeMap.get("content");
            int type = (new Integer((String)noticeMap.get("type"))).intValue();
            if(1 == type) {
                String ext = (String)noticeMap.get("ext");
                JSONObject extJson = new JSONObject(ext);
                Map extMap = JSONUtils.toMap(extJson);
                this.onSysNoticeOpened(title, content, extMap);
            } else if(2 == type) {
                HashMap extMap1 = new HashMap();
                this.onSysNoticeOpened(title, content, extMap1);
            } else {
                this.onSysNoticeOpened("","",new HashMap<String, String>());
            }
        } catch (Exception var11) {
            Log.e(TAG,"Parse json error, " , var11);
            this.onSysNoticeOpened("","",new HashMap<String, String>());
        }

    }

    /**
     * 实现通知打开回调方法，获取通知相关信息
     * @param title     标题
     * @param summary   内容
     * @param extMap    额外参数
     */
    @Override
    protected void onSysNoticeOpened(String title, String summary, Map<String, String> extMap) {
        Log.e(TAG,"OnMiPushSysNoticeOpened, title: " + title + ", content: " + summary + ", extMap: " + extMap);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("title",title);
            jsonObject.put("alert",summary);
            jsonObject.put("data",extMap);

            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK,jsonObject);
            pluginResult.setKeepCallback(true);
            CloudPushPlugin.openNotificationEvent.send(pluginResult);
        } catch (Exception e) {
            e.printStackTrace();
        }

        PackageManager packageManager = getPackageManager();
        Intent intent = packageManager.getLaunchIntentForPackage(this.getPackageName());
//        Intent intent = new Intent(Intent.ACTION_MAIN, null);
//        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        startActivity(intent);
        Log.e(intent.toString(),intent.getPackage());
        finish();
    }
}
